package org.hzz.promotions.domain.service.impl;

import io.gitee.pkmer.common.collection.CollUtil;
import io.gitee.pkmer.convention.page.PageResponse;
import io.gitee.pkmer.convention.page.query.PageQuery;
import io.gitee.pkmer.convention.service.BaseDomainService;
import lombok.Setter;
import org.hzz.promotions.domain.aggregate.CouponAggregate;
import org.hzz.promotions.domain.entity.CouponEntity;
import org.hzz.promotions.domain.entity.UserCouponEntity;
import org.hzz.promotions.domain.repository.UserCouponRepository;
import org.hzz.promotions.domain.service.CouponDomainService;
import org.hzz.promotions.domain.service.UserCouponDomainService;
import org.hzz.promotions.domain.valueobject.CouponReceive;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author <a href="mailto:1193094618@qq.com">pkmer</a>
 * <br>
 * <a href = "https://gitee.com/developeros/videos-online">Code Repository</a>
 * At 2024/9/2
 */
@Service
public class UserCouponDomainServiceImpl
        extends BaseDomainService<UserCouponRepository>
        implements UserCouponDomainService {

    @Setter(onMethod_ = @Autowired)
    private CouponDomainService couponDomainService;

    /**
     * {@inheritDoc}
     *
     * @param userId    用户id
     * @param couponIds 优惠券id 列表
     */
    @Override
    public Map<Long, CouponReceive> userReceiveCouponNum(Long userId, List<Long> couponIds) {
        List<CouponReceive> couponReceives = repository.countReceivedCouponNum(userId, couponIds);
        return couponReceives.stream().collect(Collectors.toMap(
                CouponReceive::getCouponId,
                c -> c
        ));
    }

    /**
     * {@inheritDoc}
     *
     * @param userCouponEntity 用户优惠券实体
     */
    @Override
    public void receiveCoupon(UserCouponEntity userCouponEntity) {
        repository.insertUserCoupon(userCouponEntity);
    }

    @Override
    public PageResponse<CouponAggregate> pagePage(PageQuery pageQuery) {





        PageResponse<UserCouponEntity> userCouponPages = repository.pageQueryUserCoupon(pageQuery);

        List<Long> ids = userCouponPages.getList().stream()
                .map(UserCouponEntity::getCouponId)
                .distinct()
                .toList();

        Map<Long, CouponEntity> map = couponDomainService.getCouponEntityMapById(ids);
        return Converter.INSTANCE.toPageReponse(userCouponPages, map);
    }


    @Mapper
    interface Converter{

        Converter INSTANCE = Mappers.getMapper(Converter.class);

        default PageResponse<CouponAggregate> toPageReponse(PageResponse<UserCouponEntity> page,Map<Long, CouponEntity> map){

            List<CouponAggregate> list = page.getList().stream()
                    .map(e -> toAggregate(e, map.get(e.getCouponId())))
                    .toList();

            return new PageResponse<CouponAggregate>()
                    .setCurrentPageNo(page.getCurrentPageNo())
                    .setTotal(page.getTotal())
                    .setTotalPages(page.getTotalPages())
                    .setList(list);
        }

        default CouponAggregate toAggregate(UserCouponEntity entity,CouponEntity coupon){
            return new CouponAggregate()
                    .setUserCoupon(entity)
                    .setCoupon(coupon);
        }
    }

}
